package com.burst.reptile.framework.websocket;

import java.util.List;
import java.util.Map;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.ToolUtil;

/**
 * 
 * 由于websocket的协议与Http协议是不同的， 所以造成了无法直接拿到session。
 * 重写modifyHandshake，HandshakeRequest request可以获取httpSession
 * 
 * @author wujin
 *
 * @date: 2020-9-3 12:11:26
 */
public class WebSocketConfigurator extends Configurator {

	@Override
	public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
		Map<String, List<String>> headers = request.getHeaders();
		List<String> list = headers.get(Constants.TOKEN);
		if(ToolUtil.isNotEmpty(list)) {
			sec.getUserProperties().put(Constants.TOKEN, list.get(0));
		}
	}
}
